package com.huaxinkaiye.application;

import android.app.Application;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.baidu.mapapi.SDKInitializer;
import com.huaxinkaiye.utils.BitmapCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

public class HuaXinApplication extends Application {
	public static Context applicationContext;
	public static RequestQueue mRequestQueue;
	public static com.android.volley.toolbox.ImageLoader mImageLoader;
	@Override
	public void onCreate() {
		super.onCreate();
		 applicationContext = this;
		// 在使用 SDK 各组间之前初始化 context 信息，传入 ApplicationContext
		SDKInitializer.initialize(this);
		initImageLoader(getApplicationContext());
		mRequestQueue = Volley.newRequestQueue(this);
		mImageLoader = new com.android.volley.toolbox.ImageLoader(HuaXinApplication.mRequestQueue, new BitmapCache());
	}
	  public static boolean isNetWorkConnected() {
	        ConnectivityManager cm =
	                (ConnectivityManager) applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE);
	        NetworkInfo nwi = cm.getActiveNetworkInfo();
	        if (nwi != null) {
	            return nwi.isAvailable();
	        } else {
	            return false;
	        }
	    }
	  public static void initImageLoader(Context context) {
	        // This configuration tuning is custom. You can tune every option, you may tune some of them,
	        // or you can create default configuration by
	        //  ImageLoaderConfiguration.createDefault(this);
	        // method.
	        ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder(context);
	        config.threadPriority(Thread.NORM_PRIORITY - 2);
	        config.denyCacheImageMultipleSizesInMemory();
	        config.diskCacheFileNameGenerator(new Md5FileNameGenerator());
	        config.diskCacheSize(50 * 1024 * 1024); // 50 MiB
	        config.tasksProcessingOrder(QueueProcessingType.LIFO);
//	      config.writeDebugLogs(); // Remove for release app

	        // Initialize ImageLoader with configuration.
	        ImageLoader.getInstance().init(config.build());
	    }
}